package dennis;

import java.util.ArrayList;
//import java.util.Scanner;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CommandInput_old4 {
	
	public static CommandInput staticInput;
	ArrayList<String> output = new ArrayList<String>();
	ArrayList<String> guiUpcomingList = new ArrayList<String>();
	ArrayList<Task> upcomingTenList = null;
	BasicFeaturesV1 basicFeaturesV1 = new BasicFeaturesV1();

	//constructor
	private CommandInput(){

	}
	
	public static void initInput(){
		staticInput = new CommandInput();
	}
	
	
	public void parseCmd(String command) throws Exception{
		String[] temp;
		//delimiter
				String delimiter = " ";
				//given string will be split by the argument delimiter provided.
				temp = command.split(delimiter);
				
				if(temp[0].equals("+") || temp[0].equals("+*"))
					AddTask(temp);
				else if(temp[0].equals("-"))
					//RemoveTask(temp);
					;
				else if(temp[0].equals("mod"))
					//ModifyTask(temp);
					;
				else if(temp[0].equals("view"))
					//ViewTask(temp);
					;
				else if(temp[0].equals("search"))
					//SearchTask(temp);
					;
				else if(temp[0].equals("done"))
					//DoneTask(temp);
					;
				else if(temp[0].equals("save"))
					//SaveALLTask(temp);
					;
				else
					output.add("Command error!");
				
				output.add("Complete."); //this is for testing
	}
	
    //pass Object with statements to be displayed to GUI
    public ArrayList<String> out(){
    	return output;
    	
    }
    
    //print to console instead
    public void out2console(){
    	int size = output.size();
        
		 for(int i = 0; i < size ; i++){
			 //output.add(size);
			 String astr = output.get(i);
			 System.out.println(astr);
		 }
		 output.clear();
    }
	
	public void AddTask(String[] item) throws Exception {
		
		boolean Importance = false;
		int i=0,j=0,k=0;
		char[] temp;
		String TaskDescription="";
		String tempWord="";
		//Attributes to be stored from here on
		Task task;
		int dd=0,mm=0,yy=0;
		int StartTime=0,DueTime=0;
		String StoreVenue="";
		String StoreLabel="";
		String StoreTask="";
		String ddmmyy=""; //for XML
		String StartTimeString=""; //for XML
		String DueTimeString=""; //for XML
		//Ends here
		
		if(item[0].equals("+*")) {
			Importance = true;
		}
		
		for(j=0; j<item.length; j++) {
			temp = item[j].toCharArray();
			
			if(temp[0] == '#') {
				k=1;
				tempWord="";
				while(k<temp.length) {
					tempWord = tempWord + temp[k];
					k++;
				}
				dd=ConvertDateDD(tempWord);
				mm=ConvertDateMM(tempWord);
				yy=ConvertDateYY(tempWord);
				ddmmyy = tempWord;
			}
			if(temp[0] == '@') {
				k=1;
				tempWord="";
				while(k<temp.length) {
					if(temp[k] == '-') {
						StartTime = StartTime(tempWord);
						StartTimeString = tempWord;
						tempWord = "";
						k++;
					}
					tempWord = tempWord + temp[k];
					k++;
				}
				DueTime = DueTime(tempWord);
				DueTimeString = tempWord;
			}
			if(temp[0] == '$') {
				k=1;
				tempWord="";
				while(k<temp.length) {
					tempWord = tempWord + temp[k];
					k++;
				}
				StoreVenue = tempWord;
			}
			if(temp[0] == ':') {
				k=1;
				tempWord="";
				while(k<temp.length) {
					tempWord = tempWord + temp[k];
					k++;
				}
				StoreLabel = tempWord;
			}
			if(temp[0] == '*') {
				Importance = true;
			}
			if(temp[0]!='*' && temp[0]!='#' && temp[0]!='@' && temp[0]!='$' && temp[0]!='+' && temp[0]!=':') {
				TaskDescription = TaskDescription + item[j]+' ';
			}
		}
		StoreTask = TaskDescription;
		
		/*if((!ddmmyy.isEmpty() && Character.isDigit(ddmmyy.charAt(i))) && (!StartTimeString.isEmpty() && Character.isDigit(StartTimeString.charAt(i))) && (!DueTimeString.isEmpty() && Character.isDigit(DueTimeString.charAt(i)))) {
			task = new Task(dd, mm, yy, StartTime, DueTime, StoreLabel, Importance, StoreTask, StoreVenue);
			System.out.println(task.getDate()+"lol");
			
			basicFeaturesV1.addTask(task);
			basicFeaturesV1.printeverything();
		}
		else {
			System.err.println("format error");
			throw new Exception("format not right");
		}*/
		
		task = new Task(dd, mm, yy, StartTime, DueTime, StoreLabel, Importance, StoreTask, StoreVenue);
		//System.out.println(task.getDate());
		
		basicFeaturesV1.addTask(task);
		basicFeaturesV1.printeverything();
		output.add("Successfully added.");
		
		if(SaveXML(ddmmyy, StartTimeString, DueTimeString, StoreLabel, Importance, StoreTask)) {
			//out.add("Saved!");
		}
	}
	public int StartTime(String time) {
		int IntTime=0;
		int k=3;
		char[] charTime;
		
		charTime = time.toCharArray();
		for(int i=1; i<=1000; i*=10) {
			IntTime = IntTime + (charTime[k--]-48)*i;
		}
		
		return IntTime;
	}
	public int DueTime(String time) {
		int IntTime=0;
		int k=3;
		char[] charTime;
		
		charTime = time.toCharArray();
		for(int i=1; i<=1000; i*=10) {
			IntTime = IntTime + (charTime[k--]-48)*i;
		}
		
		return IntTime;
	}
	public int ConvertDateYY(String date) {
		int yy;
		char[] ddmmyy;
		
		ddmmyy = date.toCharArray();
		yy = ((ddmmyy[4]-48)*10) + (ddmmyy[5]-48);

		return yy;
	}
	public int ConvertDateMM(String date) {
		int mm;
		char[] ddmmyy;
		
		ddmmyy = date.toCharArray();
		mm = ((ddmmyy[2]-48)*10) + (ddmmyy[3]-48);
		
		return mm;
	}
	
	public int ConvertDateDD(String date) {
		int dd;
		char[] ddmmyy;
		
		ddmmyy = date.toCharArray();
		dd = ((ddmmyy[0]-48)*10) + (ddmmyy[1]-48);

		return dd;
	}
	public static boolean SaveXML(String ddmmyy, String start, String due, String labelname, boolean impt, String task) {
		try {
			 
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	 
			// root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("Jim");
			doc.appendChild(rootElement);
	 
			// staff elements
			Element item = doc.createElement("Item");
			rootElement.appendChild(item);
	 
			// set attribute to staff element
			Attr attr = doc.createAttribute("id");
			attr.setValue("1");
			item.setAttributeNode(attr);
	 
			// shorten way
			// staff.setAttribute("id", "1");
	 
			Element Label = doc.createElement("Label");
			Label.appendChild(doc.createTextNode(labelname));
			item.appendChild(Label);
				 
			Element TaskDescription = doc.createElement("Task");
			TaskDescription.appendChild(doc.createTextNode(task)); 
			item.appendChild(TaskDescription);
	 
			Element Date = doc.createElement("Date");
			Date.appendChild(doc.createTextNode(ddmmyy));
			item.appendChild(Date);
			
			Element StartTime = doc.createElement("StartTime");
			StartTime.appendChild(doc.createTextNode(start));
			item.appendChild(StartTime);
		
			Element DueTime = doc.createElement("DueTime");
			DueTime.appendChild(doc.createTextNode(due));
			item.appendChild(DueTime);
			
			Element Importance = doc.createElement("Importance");
			if(impt)
				Importance.appendChild(doc.createTextNode("YES"));
			else
				Importance.appendChild(doc.createTextNode("NO"));
			item.appendChild(Importance);
			
			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File("file.xml"));
	 
			// Output to console for testing
			// StreamResult result = new StreamResult(System.out);
	 
			transformer.transform(source, result);
	 
			//System.out.println("Dennis-<File saved!");
	 
		  } catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		  } catch (TransformerException tfe) {
			tfe.printStackTrace();
		  }
		return true;
	}
	public ArrayList<String> upcomingTen() {
		upcomingTenList = basicFeaturesV1.retrieveUpcomingTask(2);
		Task temp = null;
		for(int i=0; i<2; i++) {
			temp = upcomingTenList.get(i);
			guiUpcomingList.add(temp.toString());
		}
	return guiUpcomingList;
	}
}